/**
 * 
 */
package com.fsoft.tinhcd.dao.impl;

import java.util.List;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.fsoft.tinhcd.dao.CustomerDao;
import com.fsoft.tinhcd.model.Customers;

/**
 * @author caodangtinh
 * 
 */
@Repository
@Transactional
public class CustomerDaoImpl implements CustomerDao {

	@Autowired
	private SessionFactory sessionFactory;

	public CustomerDaoImpl() {
	}

	@Override
	public boolean add(Customers customers) {
		try {
			sessionFactory.getCurrentSession().save(customers);
			return true;
		} catch (Exception e) {
			return false;
		}
	}

	@Override
	public boolean update(Customers customers) {
		try {
			sessionFactory.getCurrentSession().update(customers);
			return true;
		} catch (Exception e) {
			return false;
		}
	}

	@Override
	public boolean delete(Customers customers) {
		try {
			sessionFactory.getCurrentSession().delete(customers);
			return true;
		} catch (Exception e) {
			return false;
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Customers> listCustomer() {
		return (List<Customers>) sessionFactory.getCurrentSession()
				.createQuery("from Customers").list();
	}

	@Override
	public Customers getCustomerById(int customerId) {
		return (Customers) sessionFactory.getCurrentSession()
				.createQuery("from Customers c where c.customerId=:value")
				.setParameter("value", customerId).uniqueResult();
	}

}

